<?php
class mis_komplain_penugasan_model extends MY_Model {
	var $model = 'mis_komplain_penugasan';


function getApp()
	{
		$ls_sql	= "	SELECT	nama_status value,
							nama_status display
					FROM	ADMIN_PRIMKOKAS.MIS_KOMPLAIN_STATUS 
					ORDER BY kode_status";
					
		$result	= $this->db->query($ls_sql);
		if($result->num_rows() > 0){
			return $result->result_array();
		}else{
			return array();
		}
	}


	function GetJSON()
	{
		$ls_sql	= "	SELECT	a.id_komplain,a.id_penugasan,a.kode_komplain,a.id_departemen_pk,
							TO_CHAR(a.tgl_komplain,'DD/MM/YYYY') tgl_komplain,
							TO_CHAR(a.tgl_selesai,'DD/MM/YYYY') tgl_selesai,
							a.nama,a.status,a.keterangan,b.komplain,a.nama_unit
					FROM	mis_komplain_penugasan a 
					LEFT JOIN mis_komplain b ON a.kode_komplain = b.kode_komplain
					LEFT JOIN hrd_departemen c ON a.nama_unit = c.id_departemen_pk
					WHERE	(UPPER(a.kode_komplain) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.tgl_komplain) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.nama) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.status) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.keterangan) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.komplain) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.nama_unit) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(a.tgl_selesai) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'a.id_komplain' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order') );
					
		return json_encode( $this->paging($ls_sql) );
	}

	function GetJSON2()
	{
		$ls_sql	= "	SELECT	b.kode_komplain,b.nama_unit,b.id_komplain,b.komplain,c.nama_departemen,TO_CHAR(b.tgl_komplain,'DD/MM/YYYY') tgl_komplain
					FROM	mis_komplain b
					LEFT JOIN hrd_departemen c ON b.nama_unit = c.id_departemen_pk
					WHERE	(UPPER(kode_komplain) LIKE '%".strtoupper(post('keyword'))."%')
					ORDER BY ".(post('sort')=='' ? 'id_komplain' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order') );
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('mis_komplain_penugasan', $arr_data);
	}
	
	function Update($arr_data, $arr_rule){
		$this->db->update('mis_komplain_penugasan', $arr_data, $arr_rule);
	}

	function UpdateStatus($arr_data, $arr_rule){
		$this->db->update('mis_komplain', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_id_komplain){
		$result = $this->db->query("SELECT	a.id_komplain,a.id_penugasan,a.kode_komplain,a.id_departemen_pk,
											TO_CHAR(a.tgl_komplain,'DD/MM/YYYY') tgl_komplain,
											TO_CHAR(a.tgl_selesai,'DD/MM/YYYY') tgl_selesai,
											a.nama,a.status,a.keterangan,b.komplain,a.nama_unit
									FROM	mis_komplain_penugasan a 
									LEFT JOIN mis_komplain b ON a.kode_komplain = b.kode_komplain
									LEFT JOIN hrd_departemen c ON a.nama_unit = c.id_departemen_pk");
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_id_komplain)
	{
		$result = $this->db->query("SELECT	a.id_komplain,a.kode_komplain,a.id_penugasan,a.id_departemen_pk,
											TO_CHAR(a.tgl_komplain,'DD/MM/YYYY') tgl_komplain,
											TO_CHAR(a.tgl_selesai,'DD/MM/YYYY') tgl_selesai,
											a.nama,a.status,a.keterangan,b.komplain,a.nama_unit
									FROM	mis_komplain_penugasan a 
									LEFT JOIN mis_komplain b ON a.kode_komplain = b.kode_komplain
									LEFT JOIN hrd_departemen c ON a.nama_unit = c.id_departemen_pk");
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM mis_komplain_penugasan WHERE id_penugasan = '".$arr_rule['id_penugasan']."'"))<=0)
			{
				return $this->error_delete.' [Id: '.$arr_rule['id_penugasan']."]<br />";
			}else{
				$this->db->delete('mis_komplain_penugasan', array('id_penugasan' => $arr_rule['id_penugasan']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>